//
//  VoiceReadItemCell.m
//  ShowYork
//
//  Created by hxh on 2021/5/17.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "VoiceReadItemCell.h"
#import "ScrollDetailLabelView.h"
@interface VoiceReadItemCell ()
@property (nonatomic,strong)UILabel *numLb;
@property (nonatomic,strong)UILabel *timeLb;
@property (nonatomic,strong)UIImageView *playIcon;
@property (nonatomic,strong)ScrollDetailLabelView *scrollLabel;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIButton *coverBtn;
@property (nonatomic,strong)UIImageView *loadingIcon;
@end

@implementation VoiceReadItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    self.numLb = [[UILabel alloc]init];
    self.numLb.font = [UIFont boldSystemFontOfSize:16];
    self.numLb.textColor = C51;
    self.numLb.text = @"11";
    [self.contentView addSubview:self.numLb];
    
    self.numLb.frame = CGRectMake(16, 0,0, 44);
    
    self.timeLb = [[UILabel alloc]init];
    self.timeLb.text = @"03:20";
    self.timeLb.textColor = C153;
    self.timeLb.font = font(12);
    [self.contentView addSubview:self.timeLb];
    
    self.timeLb.frame = CGRectMake(0, 0, 0, 44);
    
    self.scrollLabel = [[ScrollDetailLabelView alloc]initWithFrame:CGRectZero];
    self.scrollLabel.textColor = C51;
    self.scrollLabel.textFont = font(14);
    [self.contentView addSubview:self.scrollLabel];
    
    self.titleLb = [[UILabel alloc]init];
    [self.contentView addSubview:self.titleLb];
    
    self.loadingIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_loading_1_tag"]];
    [self.contentView addSubview:self.loadingIcon];
    [self.loadingIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.timeLb);
    }];
    
    
    NSMutableArray *imageArr = [NSMutableArray array];
    for (int i = 0; i < 20; i++) {
        [imageArr addObject:[UIImage imageNamed:[NSString stringWithFormat:@"voice_loading_%i_tag",i+1]]];
    }
    self.loadingIcon.animationImages = imageArr;
    self.loadingIcon.animationDuration = 2.5;
    //动画的重复次数，想让它无限循环就赋成0
    self.loadingIcon.animationRepeatCount = 0;
    self.loadingIcon.hidden = YES;
 
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"VoiceReadItemCell";
    VoiceReadItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[VoiceReadItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setItemModel:(VoiceReadPlayerItemModel *)itemModel{
    _itemModel = itemModel;
    self.numLb.text = [NSString stringWithFormat:@"%02d",itemModel.index.intValue];
    CGSize numSize = [RPAppManager getTextSizeWithText:self.numLb.text font:[UIFont boldSystemFontOfSize:16] maxSize:CGSizeMake(kScreenW, MAXFLOAT)];
    self.numLb.cl_width = MAX(21, ceil(numSize.width));
    
    
    int totalCount = itemModel.voiceTime.intValue/1000;
    NSString *strShowTime = [NSString stringWithFormat:@"%02d:%02d",totalCount/60,totalCount%60];
    
    self.timeLb.text = strShowTime;
    CGSize timeSize = [RPAppManager getTextSizeWithText:self.timeLb.text font:font(12) maxSize:CGSizeMake(kScreenW, MAXFLOAT)];
    self.timeLb.cl_width = ceil(timeSize.width);
    self.timeLb.cl_x = kScreenW - 16 - self.timeLb.cl_width;
    
    self.scrollLabel.frame = CGRectMake(CGRectGetMaxX(self.numLb.frame) + 4, 0, kScreenW - CGRectGetMaxX(self.numLb.frame) - 4 - 20 - ceil(timeSize.width) - 16, 44);
    
    if (itemModel.status == XHPlayerListItemPlay) {
        [self setPlayStatus];
        self.timeLb.hidden = YES;
        self.loadingIcon.hidden = NO;
        [self.loadingIcon startAnimating];
    }else if (itemModel.status == XHPlayerListItemFailed ||
              itemModel.status == XHPlayerListItemNormal){
        [self setNormalStatus];
        self.timeLb.hidden = NO;
        self.loadingIcon.hidden = YES;
        [self.loadingIcon stopAnimating];
    }else{
        [self setOtherStatus];
    }
}

- (void)buttonClick:(UIButton *)btn{
    if (self.itemModel.status == XHPlayerListItemNormal || self.itemModel.status == XHPlayerListItemFailed) {
        
        self.itemModel.status = XHPlayerListItemReady;
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(playWithCurrentModel:)]) {
            [self.delegate playWithCurrentModel:self.itemModel];
        }
        [self setOtherStatus];
        
    }else{
        
    }
}
- (void)setOtherStatus{
    self.timeLb.hidden = YES;
    self.loadingIcon.hidden = NO;
    // 处理文字
    self.titleLb.hidden = NO;
    NSString *titile = [NSString stringWithFormat:@"%@-%@",self.itemModel.title,self.itemModel.author];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:titile];
    [attr addAttribute:NSForegroundColorAttributeName value:RGBA(238, 157, 59, 1) range:NSMakeRange(0, self.itemModel.title.length)];
    [attr addAttribute:NSFontAttributeName value:font(14) range:NSMakeRange(0, self.itemModel.title.length)];
    [attr addAttribute:NSForegroundColorAttributeName value:RGBA(238, 157, 59, 1) range:NSMakeRange(self.itemModel.title.length, self.itemModel.author.length+1)];
    [attr addAttribute:NSFontAttributeName value:font(12) range:NSMakeRange(self.itemModel.title.length, self.itemModel.author.length+1)];
    self.titleLb.attributedText = attr;
    self.titleLb.frame = self.scrollLabel.frame;
    self.numLb.textColor = RGBA(238, 157, 59, 1);
    
    self.scrollLabel.hidden = YES;
}
- (void)setNormalStatus{
    
    self.titleLb.hidden = NO;
    
    self.scrollLabel.hidden = YES;
    
    NSString *titile = [NSString stringWithFormat:@"%@-%@",self.itemModel.title,self.itemModel.author];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:titile];
    [attr addAttribute:NSForegroundColorAttributeName value:C51 range:NSMakeRange(0, self.itemModel.title.length)];
    [attr addAttribute:NSFontAttributeName value:font(14) range:NSMakeRange(0, self.itemModel.title.length)];
    [attr addAttribute:NSForegroundColorAttributeName value:C153 range:NSMakeRange(self.itemModel.title.length, self.itemModel.author.length+1)];
    [attr addAttribute:NSFontAttributeName value:font(12) range:NSMakeRange(self.itemModel.title.length, self.itemModel.author.length+1)];
    self.titleLb.frame = self.scrollLabel.frame;
    self.titleLb.attributedText = attr;
    self.scrollLabel.hidden = YES;
    [self.scrollLabel stopScrollView];
    
    self.numLb.textColor = C51;
}

- (void)setPlayStatus{
    self.titleLb.hidden = YES;
    
    self.scrollLabel.hidden = NO;
    
    NSString *titile = [NSString stringWithFormat:@"%@-%@",self.itemModel.title,self.itemModel.author];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:titile];
    [attr addAttribute:NSForegroundColorAttributeName value:RGBA(238, 157, 59, 1) range:NSMakeRange(0, self.itemModel.title.length)];
    [attr addAttribute:NSFontAttributeName value:font(14) range:NSMakeRange(0, self.itemModel.title.length)];
    [attr addAttribute:NSForegroundColorAttributeName value:RGBA(238, 157, 59, 1) range:NSMakeRange(self.itemModel.title.length, self.itemModel.author.length+1)];
    [attr addAttribute:NSFontAttributeName value:font(12) range:NSMakeRange(self.itemModel.title.length, self.itemModel.author.length+1)];
    self.scrollLabel.cl_width = kScreenW - CGRectGetMaxX(self.numLb.frame) - 4 - 20 - self.timeLb.cl_width - 16;
    [self.scrollLabel setAttrText:attr];
    
    self.numLb.textColor = RGBA(238, 157, 59, 1);
}


@end
